//
//  main.c
//  C_10进程与系统调用
//
//  Created by Robin on 16/3/20.
//  Copyright © 2016年 cnbm. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
//#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <string.h>

void error (char* msg){
    fprintf(stderr, "%s : %s\n",strerror(errno));
}

int main(int argc, char * argv[]) {

    char *feeds[] = {"http://feeds.appinn.com/appinns/",
                   "http://meiwenrishang.com/rss",
                   "http://songshuhui.net/feed"};

    int times = 3;
    //char *phrase = argv[1];
    
    for (int i=0; i<times; i++) {
        
        char var[255];
        
        //char *rss = feeds[i];
        sprintf(var, "RSS_FEED=%s",feeds[i]);
        char *vars[] = {var,NULL};
        
        pid_t pid = fork();
        
        if (pid == -1) {
            error("无法克隆进程");
            //fprintf(stderr, "Cannot fork the process %s\n",strerror(errno));
            return 1;
        }
        
        if (!pid) {
            
            if (execle("/Users/guorobin/Desktop/TempFile/caffe","/Users/guorobin/Desktop/TempFile/caffe","guo",NULL,vars) == -1) {
                
                error("无法运行脚本");
                //fprintf(stderr, "Warn: there must be some error %s \n!",strerror(errno));
                return 1;
            }
        }
        
        
//        // 等待子进程结束后父进程结束
//        int pid_status;
//        
//        waitpid(pid, &pid_status, 0);
//        
//        if (WEXITSTATUS(pid_status)) {
//            puts("Error status no-zero");
//        }
    }
    
    // 等待子进程结束
   
    
    return 0;
}
